package com.lnsf.dao;

import java.util.List;

import org.hibernate.dialect.Ingres10Dialect;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.lnsf.entity.User;

@Transactional
public interface UserDao extends CrudRepository<User, Integer>{

/*	@Query("select u from User u")
	public List<User> getAllUser();*/
	
	/*@Query("select u from User u where u.id = :userid")
	public User findUserById(@Param("userid") String userid);*/
	
	//通过主键id查找用户
	@Override
	@Transactional
	User findOne(Integer userid);
	
	//通过手机号码来确认注册用户 信息是否被注册（一台手机一个用户）,以及模糊查询
	@Query("select u from User u where u.phone = :phone")
	User findByPhone(@Param("phone") String phone);
	
	//根据手机号码以及密码登陆
	User findByPhoneAndPassword(String phone , String password);
	
	//查找全部用户
	@Override
	@Transactional
	Iterable<User> findAll();
	
	//保存用户
	@Override
	@Transactional
	<S extends User> S save(S arg0);

	//删除用户
	@Override
	@Transactional
	void delete(User user);
	
	// 使用query 注解进行update 或者 delete 语句时，需要添加 modifying 注解修饰  
    @Query(value="update User u set u.username = :username where u.id = :userid ")  
    @Modifying  
    public void updateUser(@Param("username" ) String username ,@Param("userid") Integer userid); 
	
    //获取用户总数
   @Override
   long count();
   
   //根据用户名查找，模糊查找
   List<User> findByUsernameLike(String username);
   
   //修改用户激活状态
   @Modifying
   @Query(value="update User u set u.active = 1 where u.code = :code ")
   int activeUser(@Param("code" ) String code);
   
}
